#include <iostream>
#include <queue>
using namespace std;
struct Node{
    int id,s;
};
int win[100005][25];
char dg[100005][25];
int square[25];
int LOG2[200005];
int c[100005],a[100005];
queue <Node> q;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena,out","w",stdout);
    square[0]=1,LOG2[0]=-1;
    for(int i=1;i<=25;i++) square[i]=square[i-1]*2;
    for(int i=1;i<=200000;i++) LOG2[i]=LOG2[i/2]+1;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int k=0;k<=LOG2[n*2-1];k++){
        for(int i=1;i<=square[LOG2[n*2-1]-k];i++){
            cin>>dg[i][k];
        }
    }
    for(int i=1;i<=m;i++) cin>>c[i];
    int t;
    cin>>t;
    for(int k=1;k<=t;k++){
        int x[5];
        cin>>x[1]>>x[2]>>x[3]>>x[4];
        for(int i=1;i<=n;i++)a[i]^=x[i%4];
        int turn=1;
        for(int i=1;i<=n;i++) q.push({i,a[i]});
        while(q.size()!=1){
            Node x=q.front();
            q.pop();
            Node y=q.front();
            if(y.id>n-square[turn-1]) turn++;
            q.pop();
            if(dg[y.id/square[turn]][turn]=='1'){
                if(y.s>=turn){
                    q.push(y);
                    win[y.id/square[turn]][turn]=y.id;
                }
                else{
                    q.push(x);
                    win[y.id/square[turn]][turn]=x.id;
                }
            }
            else{
                if(x.s>=turn){
                    q.push(x);
                    win[y.id/square[turn]][turn]=x.id;
                }
                else{
                    q.push(y);
                    win[y.id/square[turn]][turn]=y.id;
                }
            }
        }
        int ans=0;
        for(int i=1;i<=m;i++){
            ans^=(i*win[1][LOG2[c[i]]]);
        }
        cout<<ans<<endl;

    }

    return 0;
}
